package inc.zhugastrov.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import inc.zhugastrov.sellerform.BasketPanel;
import inc.zhugastrov.sellerform.CurrentOrdersPane;
import inc.zhugastrov.sellerform.MainPanel;
import inc.zhugastrov.sellerform.WherehousePane;
import inc.zhugastrov.ui.resourses.UserViewTableModel;
import inc.zhugastrov.users.Account;
import inc.zhugastrov.users.Position;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import static inc.zhugastrov.dbactions.DBActions.fillOrders;
import static inc.zhugastrov.dbactions.DBActions.fillItems;
import static inc.zhugastrov.dbactions.DBActions.*;

public class UserManagment extends JFrame {
	private final String[] columnNames = { "Name", "Surname", "Position" };
	private JTable table;
	private JScrollPane scroller;
	private static UserViewTableModel tableModel;
	private JButton addUser;
	private JTabbedPane tabbedPane;
	private JPanel pannel;
	private MainPanel mainPanel;
	private WherehousePane wherehousePane;
	private CurrentOrdersPane orders;
	public UserManagment() {
		pannel = new JPanel();
		initComponent();
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			this.setTitle("Staff");
			this.setSize(600, 600);
			this.setContentPane(tabbedPane);
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			this.setVisible(true);
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static UserViewTableModel getModel(){
		return tableModel;
	}
	
	
	private void initComponent() {
		//initDB();
		fillItems();
		fillOrders();
		
		Account testAcc = new Account();
		
		
		testAcc.setName("Alex");
		testAcc.setSalary(100d);
		testAcc.setPassword("1111");
		testAcc.setPassportNumber("ME23221");
		testAcc.setLogin("user");
		testAcc.setPosition(Position.SELLER);
		testAcc.setSurname("Zhugastrov");
		mainPanel = MainPanel.getInstance();
		orders = CurrentOrdersPane.getInstance();
		wherehousePane = WherehousePane.getInstance();
		tabbedPane = new JTabbedPane();
		tabbedPane.addTab("User Managment", pannel);
		tabbedPane.addTab("Items Managment", mainPanel);
		tabbedPane.addTab("Orders Managment", orders);
		tabbedPane.addTab("Warehouse", wherehousePane);
		add(tabbedPane);
		
		tableModel = new UserViewTableModel(columnNames);
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					new UserUpdateForm(
							tableModel.getRow(table.getSelectedRow()));
				}
			}

		});

		table.setModel(tableModel);
		table.setFocusable(false);
		tableModel.addEmptyRow();
		tableModel.addRow(testAcc);

		scroller = new JScrollPane(table);
		table.setPreferredScrollableViewportSize(new Dimension(500, 300));
			
		pannel.add(scroller, BorderLayout.CENTER);
		
		addUser = new JButton("Create new user");
		addUser.setSize(50, 20);
		addUser.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new UserUpdateForm();
			}
		});
		fillUsers();
		pannel.add(addUser);
	
	}
//	
//	public static void main(String[] args) {
//		try {
//			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		new UserManagment();
//	}

}
